package com.itaq.cheetah.serviceA.config.web.resolver;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.core.type.TypeReference;
import com.itaq.cheetah.serviceA.config.anno.VerifySign;
import com.itaq.cheetah.serviceA.config.exception.CustomException;
import com.itaq.cheetah.serviceA.config.factory.ObjectMapperFactory;
import com.itaq.cheetah.serviceA.config.web.AuthContextHolder;
import com.itaq.cheetah.serviceA.config.web.dto.SsoSignSource;
import com.itaq.cheetah.serviceA.dao.ISsoClientDetailMapper;
import com.itaq.cheetah.serviceA.model.entity.SsoClientDetail;
import com.itaq.cheetah.serviceA.utils.JsonUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;


/**
 * @author cheetah
 * @date 2022/07/25
 */
@Slf4j
@AllArgsConstructor
public class VefifySignArgumentResolver implements HandlerMethodArgumentResolver {

    private final ISsoClientDetailMapper iSsoClientDetailMapper;

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        log.info("进入验签判断");
        boolean b = parameter.hasParameterAnnotation(VerifySign.class);
        return b;
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
        BufferedReader reader = servletRequest.getReader();
        StringBuilder sb = new StringBuilder();
        String str = null;
        while ((str = reader.readLine()) != null) {
            sb.append(str);
        }
        SsoSignSource signSource = JsonUtils.read(sb.toString(), new TypeReference<SsoSignSource>() {
        });
        SsoClientDetail ssoClientDetail = iSsoClientDetailMapper.selectOne(
                new LambdaQueryWrapper<SsoClientDetail>()
                        .eq(SsoClientDetail::getPlatformId, signSource.getPlatformId())
        );
        String appSecret = ssoClientDetail.getPlatformSecret();
        signSource.setPlatformSecret(appSecret);
        String sign = signSource.sign();
        log.info("sign source raw data={}", JsonUtils.toPrettyString(signSource));
        log.info("sign result={}", sign);
        log.info("sign source={}", signSource.getSign());
        if (sign.equals(signSource.getSign())) {
            log.info("签名通过");
        } else {
            throw new CustomException("签名错误");
        }
        AuthContextHolder.getInstance().setContext(ssoClientDetail);
        return ObjectMapperFactory.getSecurityObjectMapper().readValue(sb.toString(), parameter.getParameterType());
    }
}
